package com.tools.controller;

import com.tools.common.beans.constant.ControllerConstant;
import com.tools.common.beans.vo.Result;
import com.tools.generator.entity.BaseClassEntity;
import com.tools.generator.service.BaseClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

/**
 * @author Admin
 */
@RestController
@RequestMapping(ControllerConstant.API_BASE_ENTITY_PREFIX)
public class BaseClassController extends BaseController {

    @Autowired
    private BaseClassService baseClassService;

    /**
     * 基础路径
     */
    private final String prefix = "generate/";



    @GetMapping(value = {"/{page}.html"})
    public ModelAndView allPage(@PathVariable("page") String page){
        return jumpPage(prefix + page);
    }

    @GetMapping("baseEntity-edit")
    public ModelAndView editPage(@RequestParam Long id){
        BaseClassEntity byId = baseClassService.getById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("baseEntity",byId);
        modelAndView.setViewName(prefix + "baseEntity-edit");
        return modelAndView;
    }

    @PostMapping("page")
    public Result page(BaseClassEntity query) {
        startPage(query.getPage(), query.getLimit());
        List<BaseClassEntity> page = baseClassService.page(query);
        return Result.OK(page);
    }

    @GetMapping("list")
    public Result<List<BaseClassEntity>> list() {
        List<BaseClassEntity> list = baseClassService.getList();
        return Result.OK(list);
    }

    @GetMapping("{id}")
    public Result<BaseClassEntity> get(@PathVariable("id") Long id) {
        BaseClassEntity data = baseClassService.getById(id);
        return Result.OK(data);
    }

    @PostMapping("save")
    public Result save(@RequestBody BaseClassEntity entity) {
        baseClassService.save(entity);
        return Result.OK(1);
    }

    @PutMapping("update")
    public Result update(@RequestBody BaseClassEntity entity) {
        baseClassService.updateById(entity);
        return Result.OK(1);
    }

    @DeleteMapping("delete")
    public Result delete(@RequestBody Long[] ids) {
        baseClassService.removeBatchByIds(Arrays.asList(ids));
        return Result.OK(1);
    }
}
